package com.ambimmort.shj.service.generalfile;

import cn.net.greenet.common.db_service.BaseDao;
import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Webcategory;
import cn.net.greenet.db.bean.WebcategoryId;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.List;

/**
 * Created by 师梁 on 2015/7/30.
 */
@Service("add_WebService")
public class Add_WebService {
//    @Autowired
//    private SessionFactory hibernateSessionFactory;
    ResultSet rs = null;
    BaseDao baseDao = null;
    String sql = null;

    private void init() {
        baseDao = new BaseDao();
    }

    private void close() {
        if (baseDao != null) {
            baseDao.close();
        }
    }
    public  boolean add_Web(String content){

        JSONArray arr = JSONArray.fromObject(content);
         init();
        boolean result = false;
        try {
            Webcategory web = null;
            WebcategoryId webid = null;
            for (int i = 0; i < arr.size(); i++) {
                JSONObject one = arr.getJSONObject(i);
                String hostName = one.getString("hostName");
                String webTypeId = one.getString("WebTypeId");
                String webTypeName = one.getString("WebTypeName");
                sql = "select * From Webcategory as we where we.id.hostName = '" + hostName + "' "
                        + "and we.id.webTypeId =" + Integer.parseInt(webTypeId)
                        + " and we.webTypeName ='" + webTypeName + "'";
                if (!rs.next()) {
                    Object[] param ={hostName,webTypeId,webTypeName};
                    sql = "insert into Webcategory (hostName, WebTypeID,WebTypeName ) values (?, ?, ?)";
                    baseDao.executeUpdate(sql, param);
                }
            }
            result = true;
        } finally {
            close();
//            sf.close();
            return result;
        }
    }
//    JSONArray arr = JSONArray.fromObject(content);
//    Session session = hibernateSessionFactory.openSession();
//    session.clear();
//    boolean result = false;
//    try {
//        Webcategory web = null;
//        WebcategoryId webid = null;
//        session.clear();
//        Transaction tx = session.beginTransaction();
//        for (int i = 0; i < arr.size(); i++) {
//            JSONObject one = arr.getJSONObject(i);
//            String hostName = one.getString("hostName");
//            String webTypeId = one.getString("WebTypeId");
//            String webTypeName = one.getString("WebTypeName");
//            String hql = "From Webcategory as we where we.id.hostName = '" + hostName + "' "
//                    + "and we.id.webTypeId =" + Integer.parseInt(webTypeId)
//                    + " and we.webTypeName ='" + webTypeName + "'";
//            List list = session.createQuery(hql).list();
//            System.out.println("Size:" + list.size());
//            if (list.isEmpty()) {
//                web = new Webcategory();
//                webid = new WebcategoryId();
//                webid.setHostName(hostName);
//                webid.setWebTypeId(Integer.parseInt(webTypeId));
//                web.setId(webid);
//                web.setWebTypeName(webTypeName);
//                session.save(web);
//            }
//        }
//        tx.commit();
//        result = true;
//    } finally {
//
//        session.close();
////            sf.close();
//        return result;
//    }
//}
}
